package com.myspace.server.blog.controller;

import com.myspace.server.base.entity.CustomPage;
import com.myspace.server.base.util.JsonView;
import com.myspace.server.base.util.JsonViewFactory;
import com.myspace.server.blog.domain.ArticleArchiveDay;
import com.myspace.server.blog.service.ArticleDetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 文章控制器
 * @author peng.zhang
 */
@Api(tags = {"blog", "blog-ArticleDetailController"})
@RestController
@RequestMapping(value = "/blog/article")
public class ArticleDetailController {

    private ArticleDetailService articleDetailService;

    @Autowired
    public ArticleDetailController(ArticleDetailService articleDetailService) {
        this.articleDetailService = articleDetailService;
    }

    @ApiOperation(value = "文章列表", notes = "根据分页条件查询文章列表！")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页", required = true, paramType = "query",
                    dataTypeClass = Integer.class, dataType = "Integer"),
            @ApiImplicitParam(name = "size", value = "查询条数", required = true, paramType = "query",
                    dataTypeClass = Integer.class, dataType = "Integer")
    })
    @GetMapping
    public JsonView getArticleList(@Param("page") Integer page, @Param("size") Integer size) {
        JsonView jsonView = JsonViewFactory.createJsonView();
        try {
            CustomPage customPage = CustomPage.builder().page(page).size(size).build();
            jsonView.successPack(articleDetailService.getArticleList(customPage));
        } catch (Exception e) {
            jsonView.errorPack("getArticleList(/blog/article) error", e);
        }
        return jsonView;
    }

    @ApiOperation(value = "分类文章列表", notes = "根据分类及分页条件查询文章列表！")
    @ApiImplicitParam(name = "categoryId", value = "类别 ID", required = true, paramType = "path",
            dataTypeClass = Long.class, dataType = "Long")
    @GetMapping(value = "/category/{categoryId}")
    public JsonView getArticleListByCategory(@PathVariable Long categoryId) {
        JsonView jsonView = JsonViewFactory.createJsonView();
        try {
            jsonView.successPack(articleDetailService.getArticleListByCategory(categoryId));
        } catch (Exception e) {
            jsonView.errorPack("getArticleListByCategory(/blog/article/category/{categoryId}) error", e);
        }
        return jsonView;
    }

    @ApiOperation(value = "文章详情", notes = "根据文章 ID 查询文章详情！")
    @ApiImplicitParam(name = "articleId", value = "文章 ID", required = true, paramType = "path",
            dataTypeClass = Long.class, dataType = "Long")
    @GetMapping(value = "/{articleId}")
    public JsonView getArticleDetail(@PathVariable Long articleId) {
        JsonView jsonView = JsonViewFactory.createJsonView();
        try {
            jsonView.successPack(articleDetailService.getArticleById(articleId));
        } catch (Exception e) {
            jsonView.errorPack("getArticleDetail(/blog/article/{articleId}) error", e);
        }
        return jsonView;
    }

    @ApiOperation(value = "最新文章列表", notes = "根据分页条件查询最新文章列表！")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页", required = true, paramType = "query",
                    dataTypeClass = Integer.class, dataType = "Integer"),
            @ApiImplicitParam(name = "size", value = "返回条数", required = true, paramType = "query",
                    dataTypeClass = Integer.class, dataType = "Integer")
    })
    @GetMapping(value = "/latest")
    public JsonView getLatestArticleList(@Param("page") Integer page, @Param("size") Integer size) {
        JsonView jsonView = JsonViewFactory.createJsonView();
        try {
            CustomPage customPage = CustomPage.builder().page(page).size(size).build();
            jsonView.successPack(articleDetailService.getArticleList(customPage));
        } catch (Exception e) {
            jsonView.errorPack("getLatestArticle(/blog/article/latest/{num}) error", e);
        }
        return jsonView;
    }

    @ApiOperation(value = "文章天统计", notes = "对文章进行每天数量统计！")
    @GetMapping(value = "/archive/day")
    public JsonView getArticleArchiveListDay() {
        JsonView jsonView = JsonViewFactory.createJsonView();
        try {
            List<ArticleArchiveDay> archiveList = articleDetailService.getArticleArchiveListDay();
            jsonView.successPack(archiveList);
        } catch (Exception e) {
            jsonView.errorPack("getArticleArchiveList(/blog/article/archive) error", e);
        }
            return jsonView;
        }
}
